﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data;

namespace Server
{
    public class Actor
    {
        public Account user;
        public SocketClient client;

        public Actor(Account user, SocketClient client)
        {
            this.user = user;
            this.client = client;
        }
    }

    public class ActorManager
    {
        private static ActorManager _instance;
        public static ActorManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ActorManager();
                }
                return _instance;
            }
        }


        public Dictionary<string, Actor> actors = new Dictionary<string, Actor>();

        /// <summary>
        /// 添加新玩家
        /// </summary>
        /// <param name="req"></param>
        /// <param name="client"></param>
        public void Add(Account acc,SocketClient client)
        {
            actors.Add(acc.username,new Actor(acc, client));
        }

        /// <summary>
        /// 移除玩家
        /// </summary>
        /// <param name="username"></param>
        public void Remove(SocketClient client)
        {
            string username=null;
            foreach (var item in actors)
            {
                if(item.Value.client==client)
                {
                    username = item.Key;
                }
            }
            if(username!=null)
            {
                LogManager.Log(username + "退出游戏");

                Actor actor = actors[username];
                actors.Remove(username);

                GameServer.Instance.SendOther(client,Protocal.LogOut_CMD, actor.user);
            }
        }

        /// <summary>
        /// 是否已存在登录玩家
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public bool HasActor(ReqLogin req)
        {
            if(actors.ContainsKey(req.username))
            {
                return true;
            }
            foreach (var item in actors)
            {
                if(item.Value.user.nickname==req.nickname)
                {
                    return true;
                }
            }
            return false;
        }

        //获取所有登录玩家的登录信息
        public List<Account> GetAllLogins()
        {
            List<Account> list = new List<Account>();
            foreach (var item in actors)
            {
                list.Add(item.Value.user);
            }
            return list;
        }

        public Account GetActor(string username)
        {
            return actors[username].user;
        }

        public Account GetActor(SocketClient client)
        {
            foreach (var item in actors)
            {
                if(item.Value.client==client)
                {
                    return item.Value.user;
                }
            }
            return null;
        }
    }

}
